$(".chargeBtn > button").eq(0).css("background", "#dddddd")
$(".chargeBtn > button").off('click').on('click', function () {
    var index = $(this).index();
    $(this).css("background", "#dddddd").siblings().css("background", "white");
    $(".chargeContent > div").eq(index).show().siblings().hide()
});

function showDetail(obj) {
    $(obj).parent().siblings().toggle();
    $(obj).children().toggle();
}

function selectAll() {
    if ($("#checkall").prop("checked")) {
        $("input[type='checkbox'][name='checkedres']").prop("checked", true);//全选
        chooseArrearsDeal();
    } else {
        $("input[type='checkbox'][name='checkedres']").prop("checked", false);  //取消全选
        chooseArrearsDeal();
    }
};

function chooseArrearsDeal(obj) {
    $("#aTcName option[value='" + $(obj).parents('.arrearsDeal').find('.atcName').html() + "']").attr("selected", true).siblings().attr('selected', false);
    $("#arrearsAmount").val($(obj).parents('.arrearsDeal').find('.arrears').html());
    $("#chooseDealArrears").html($(obj).parents('.arrearsDeal').find('.arrears').html());
};

function changeAmount() {
    if (parseFloat($("#arrearsAmount").val()) > parseFloat($("#chooseDealArrears").html())) {
        layer.msg('支付金额不能超过待缴欠费金额！', {time: 1000, icon: 2});
        $("#arrearsAmount").val($("#chooseDealArrears").html());
    }
    ;
};

var countNum = 0;

//补欠费
function repairArrears() {
    countNum++;
    if (countNum == 1) {
        var id = $("#customerId").val();
        var isRefundAdmin = $("#isRefundAdminValue").val();
        var outpatientNamed = $('#outpatientNamed', parent.parent.document).val();
        $.ajax({
            url: Feng.ctxPath + "/consultant/getCustomerStatus/" + $("#customerId").val(),
            dataType: "json",
            contentType: "application/json",
            type: "POST",
            success: function (data) {
                if (data.split(",")[0] == '复诊') {
                    alert("复诊不能补欠款！");
                    countNum = 0;
                    return false;
                } else if ($("#aTcName").val() != data.split(",")[3] && data.split(",")[2] != 0) {
                    alert("患者TC不是当前选中TC，请联系前台分配患者！");
                    countNum = 0;
                    return false;
                } else if (data.split(",")[1] > 0) {
                    alert("此患者还有未结账的单，不能补欠款！");
                    countNum = 0;
                    return false;
                } else {
                    if (checkIsYesterdayCust()) {
                        var billList = [];
                        var reservationsId = $("#reservationsId").val();
                        var arrearsAmount = $("#arrearsAmount").val();
                        var arrearsRemark = $("#arrearsRemark").val();
                        var arrearsDealList = $(".arrearsDeal");
                        var operationTimes = "";
                        var arrearsOutpatientName = "";
                        for (var i = 0; i < arrearsDealList.length; i++) {
                            var arrearsDeal = arrearsDealList[i];
                            if ($(arrearsDeal).find(".id").is(':checked')) {
                                arrearsOutpatientName = $(arrearsDeal).find(".arrearsOutpatientName").html();
                                if (isRefundAdmin == 0) {
                                    if (outpatientNamed == arrearsOutpatientName) {
                                        operationTimes = $(arrearsDeal).find(".id").val();
                                    } else {
                                        alert("不允许操作其他门店的记录！");
                                        countNum = 0;
                                        return false;
                                    }
                                } else {
                                    operationTimes = $(arrearsDeal).find(".id").val();
                                }
                            }
                        }
                        if ($("#inquireAbout").find('.inquireAbout').length > 0) {
                            for (var e = 0; e < $("#inquireAbout").find('.inquireAbout').length; e++) {
                                var obj = $("#inquireAbout").find('.inquireAbout').eq(e);
                                var dealInfo = {};
                                dealInfo.customerId = id;
                                dealInfo.reservationsId = reservationsId;
                                dealInfo.arrearsAmount = arrearsAmount;
                                dealInfo.arrearsRemarks = arrearsRemark;
                                dealInfo.tcName = $("#aTcName").val();
                                dealInfo.operationTime = operationTimes;
                                dealInfo.depositAmount2 = $(obj).find('.depositAmount2').val();
                                dealInfo.deductionType2 = $(obj).find('.deductionType2').val();
                                dealInfo.deductionAmount2 = $(obj).find('.deductionAmount2').val();
                                dealInfo.id2 = $(obj).attr('id');
                                billList.push(dealInfo);
                            }
                            $.ajax({
                                url: Feng.ctxPath + "/consultant/repairArrearsYh",
                                dataType: "json",
                                contentType: "application/json",
                                data: JSON.stringify(billList),
                                type: "POST",
                                success: function (data) {
                                    var isDorj = $("#isDorj").val();
                                    if (data > 0) {
                                        if (isDorj == "dd") {
                                            alert("操作成功!");
                                            window.location.reload();
                                        } else {
                                            layer.alert("操作成功!");
                                            if($("#detail").val()=='isArrive') {
                                                location.href = Feng.ctxPath + "/consultantDetails/consultantToDetailByTab/" + id + '/2/0';
                                            }else{
                                                location.href = Feng.ctxPath + "/consultantDetails/consultantToDetailByTab/" + id + '/3/0';
                                            }
                                        }
                                    } else {
                                        if (isDorj == "dd") {
                                            alert('权限被限制，请联系管理员!');
                                            window.location.reload();
                                        } else {
                                            alert('权限被限制，请联系管理员!');
                                            if($("#detail").val()=='isArrive') {
                                                location.href = Feng.ctxPath + "/consultantDetails/consultantToDetailByTab/" + id + '/2/0';
                                            }else{
                                                location.href = Feng.ctxPath + "/consultantDetails/consultantToDetailByTab/" + id + '/3/0';
                                            }
                                        }
                                    }
                                }
                            });
                        } else {
                            $.ajax({
                                url: Feng.ctxPath + "/consultant/repairArrears",
                                dataType: "json",
                                data: {
                                    "operationTimes": operationTimes,
                                    "reservationsId": reservationsId,
                                    "arrearsRemark": arrearsRemark,
                                    "arrearsRemark": arrearsRemark,
                                    "tcName": $("#aTcName").val(),
                                    "customerId": id,
                                    "arrearsAmount": arrearsAmount,
                                    "arrearsOutpatientName": arrearsOutpatientName
                                },
                                type: "POST",
                                success: function (data) {
                                    var isDorj = $("#isDorj").val();
                                    if (data > 0) {
                                        if (isDorj == "dd") {
                                            alert("操作成功!");
                                            window.location.reload();
                                        } else {
                                            layer.alert("操作成功!");
                                            if($("#detail").val()=='isArrive') {
                                                location.href = Feng.ctxPath + "/consultantDetails/consultantToDetailByTab/" + id + '/2/0';
                                            }else{
                                                location.href = Feng.ctxPath + "/consultantDetails/consultantToDetailByTab/" + id + '/3/0';
                                            }
                                        }
                                    } else {
                                        if (isDorj == "dd") {
                                            alert('权限被限制，请联系管理员!');
                                            window.location.reload();
                                        } else {
                                            alert('权限被限制，请联系管理员!');
                                            if($("#detail").val()=='isArrive') {
                                                location.href = Feng.ctxPath + "/consultantDetails/consultantToDetailByTab/" + id + '/2/0';
                                            }else{
                                                location.href = Feng.ctxPath + "/consultantDetails/consultantToDetailByTab/" + id + '/3/0';
                                            }
                                        }
                                    }
                                }
                            });
                        }
                    } else {
                        alert("不允许操作昨天的数据,需要前台重咨,要计算门诊人数！");
                        countNum = 0;
                        return false;
                    }

                }
            }
        });
    }
};

openingDetails = function (operationTime, outpatientName) {
    var customerId = $("#customerId").val();
    var reservationsId = $("#reservationsId").val();
    var index = layer.open({
        type: 2,
        title: '开单详情',
        area: ['85%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultant/openingDetails/' + operationTime + '/' + customerId + '/' + reservationsId + '/' + outpatientName
    });
    this.layerIndex = index;
};

openingDetailAlone = function (operationTime, outpatientName, getTime) {
    var customerId = $("#customerId").val();
    var reservationsId = $("#reservationsId").val();
    var index = layer.open({
        type: 2,
        title: '开单详情',
        area: ['85%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultant/openingDetailAlone/' + operationTime + '/' + customerId + '/' + reservationsId + '/' + outpatientName + '/' + getTime
    });
    this.layerIndex = index;
};

detailsPrint = function (operationTime, outpatientName) {
    var reservationsId = $("#reservationsId").val();
    var index = layer.open({
        type: 2,
        title: '打印详情',
        area: ['70%', '80%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultant/to_printP/' + operationTime + '/' + reservationsId + '/' + outpatientName
    });
    this.layerIndex = index;
};

printingDeal = function (operationTime, outpatientName, getTime, reservationsId) {
    var reservationsId = $("#reservationsId").val();
    var index = layer.open({
        type: 2,
        title: '开单详情',
        area: ['85%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultant/to_prints/' + operationTime + '/' + reservationsId + '/' + outpatientName + '/' + getTime
    });
    this.layerIndex = index;
};

//变更医生
function updateAchievement(operationTime, outpatientName) {
    var time = firstMonthTime(new Date(), 0) + " " + "00:00:00";
    var lastTime = setDateTime(new Date(time), 1) + " " + "16:00:00";
    var start = new Date(lastTime.replace("-", "/").replace("-", "/"));
    var end = new Date(operationTime.replace("-", "/").replace("-", "/"));
    var roleNameLogin = $("#roleNameLogin").val();
    if (roleNameLogin != "超级管理员") {
        if (start > end) {
            layer.alert("只能变更本月数据，超出请联系数据中心！！");
            return;
        }
    }
    var outpatientNamed = $('#outpatientNamed', parent.parent.document).val();
    var reservationsId = $("#reservationsId").val();
    var customerId = $("#customerId").val();
    if (outpatientNamed == outpatientName) {
        var index = layer.open({
            type: 2,
            title: '变更医生',
            area: ['70%', '80%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/consultant/updateAchievement/' + operationTime + '/' + customerId + '/' + reservationsId
        });
        this.layerIndex = index;
    } else {
        alert("不允许操作其他门店的记录！");
        return false;
    }
    ;
};

//变更医生2
function updateDoctorDeal(getTime, outpatientName) {
    var time = firstMonthTime(new Date(), 0) + " " + "00:00:00";
    var lastTime = setDateTime(new Date(time), 1) + " " + "16:00:00";
    var start = new Date(lastTime.replace("-", "/").replace("-", "/"));
    var end = new Date(getTime.replace("-", "/").replace("-", "/"));
    var roleNameLogin = $("#roleNameLogin").val();
    if (roleNameLogin != "超级管理员") {
        if (start > end) {
            layer.alert("只能变更本月数据，超出请联系数据中心！！");
            return;
        }
    }
    var outpatientNamed = $('#outpatientNamed', parent.parent.document).val();
    var reservationsId = $("#reservationsId").val();
    var customerId = $("#customerId").val();
    if (outpatientNamed == outpatientName) {
        var index = layer.open({
            type: 2,
            title: '变更医生',
            area: ['40%', '40%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/consultant/updateDoctorDeal/' + getTime + '/' + customerId + '/' + reservationsId
        });
        this.layerIndex = index;
    } else {
        alert("不允许操作其他门店的记录！");
        return false;
    }
};

//修改开单备注
function updateRemarksDeal(getTime, outpatientName) {
    var outpatientNamed = $('#outpatientNamed', parent.parent.document).val();
    var reservationsId = $("#reservationsId").val();
    var customerId = $("#customerId").val();
    if (outpatientNamed == outpatientName) {
        var index = layer.open({
            type: 2,
            title: '修改开单备注',
            area: ['40%', '40%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/consultant/updateRemarksDeal/' + getTime + '/' + customerId + '/' + reservationsId
        });
        this.layerIndex = index;
    } else {
        alert("不允许操作其他门店的记录！");
        return false;
    }
};


var refundcount = 0;
refundTotal = function (operationTime, serviceActualPay, depositAmount, outpatientName, tcName) {
    refundcount++;
    if (refundcount == 1) {
        var userName = $("#userName").val();
        var roleNameLogin = $("#roleNameLogin").val();
        var dealyesLogin = $("#dealyesLogin").val();
        if (userName != tcName) {  //登录人不是开单Tc
            if ((roleNameLogin.indexOf("前台") != -1 && (tcName == "分诊TC" || tcName == "待定TC")) || dealyesLogin == "1") {  //登录人是前台
            } else {
                alert("您不是开单tc不能退款！");
                refundcount = 0;
                return;
            }
        }
        var outpatientNamed = $('#outpatientNamed', parent.parent.document).val();
        var customerId = $("#customerId").val();
        $.ajax({
            url: Feng.ctxPath + "/consultant/getCustomerStatus/" + $("#customerId").val(),
            dataType: "json",
            contentType: "application/json",
            type: "POST",
            success: function (data) {
                if (data.split(",")[0] == '复诊') {
                    alert("复诊不能退款！请重咨")
                    refundcount = 0;
                    return;
                } else if (data.split(",")[1] > 0) {
                    alert("此患者还有未结账的单，不能退款！");
                    refundcount = 0;
                    return;
                } else if (data.split(",")[2] == 3) {
                    alert("患者TC不是当前登录人，请联系前台分配患者！");
                    refundcount = 0;
                    return;
                } else {
                    if (outpatientNamed == outpatientName) {
                        if (checkIsYesterdayCust()) {
                            refundcount = 0;
                            // if ((parseFloat(serviceActualPay) + (parseFloat(depositAmount)) > 0)) {
                            var reservationsId = $("#reservationsId").val();
                            var index = layer.open({
                                type: 2,
                                title: '退款',
                                area: ['55%', '100%'], //宽高
                                fix: true, //不固定
                                maxmin: true,
                                offset: ['0px', '45%'],
                                content: Feng.ctxPath + '/consultant/refundTotal/' + operationTime + '/' + reservationsId + '/' + customerId
                            });
                            this.layerIndex = index;
                            // } else {
                            //     layer.msg('实收金额为0，不能进行退款操作！', {time: 1000, icon: 2});
                            //     return false;
                            // }
                        } else {
                            refundcount = 0;
                            alert("不允许操作昨天的数据,需要前台重咨,要计算门诊人数！");
                            return false;
                        }
                    } else {
                        refundcount = 0;
                        alert("不允许操作其他门店的记录!");
                        return false;
                    }
                }
            }
        });
    } else {
    }
};

function applicationWay2(obj) {
    if ($(obj).find("option:selected").text() != "积分") {
        $.ajax({
            url: Feng.ctxPath + "/payService/getCouponPrice",
            type: "POST",
            data: {
                id: $(obj).val()
            },
            success: function (data) {
                var amount = parseFloat($(obj).parent().parent().find(".amount2").html());
                $(obj).parent().parent().find(".deductionAmount2").attr("readonly", true);
                if ($('#earnests').is(':checked')) {
                    var depositAmount = parseFloat($(obj).parent().parent().find(".depositAmount2").val());
                    if (data > (amount - depositAmount)) {
                        $(obj).parent().parent().find(".deductionAmount2").val(amount - depositAmount);
                    } else {
                        $(obj).parent().parent().find(".deductionAmount2").val(data);
                    }
                } else {
                    if (data > amount) {
                        $(obj).parent().parent().find(".deductionAmount2").val(amount);
                    } else {
                        $(obj).parent().parent().find(".deductionAmount2").val(data);
                    }
                }
                countMoney();
            }
        })
    } else {
        $(obj).parent().parent().find(".deductionAmount2").attr("readonly", false).val(0);
        countMoney();
    }
}

function changeDeductionAmount2(obj) {
    var deductionAmount2 = parseFloat($(obj).val());
    var jfbalance2 = parseFloat($("#jfbalance2").html());
    if (jfbalance2 > 0) {
        var totalSum = 0;
        $("#inquireAbout .deductionAmount2").each(function (index, obj) {
            if ($(obj).parent().parent().find('.deductionType2').find("option:selected").text() == '返现抵扣') {
                totalSum += parseFloat($(obj).val());
            }
        });
        if (isNaN(deductionAmount2) != true) {
            var amount = parseFloat($(obj).parent().parent().find(".amount2").html());
            if ($('#earnests').is(':checked')) {
                var depositAmount2 = parseFloat($(obj).parent().parent().find(".depositAmount2").val());
                if ((deductionAmount2 + depositAmount2) > amount) {
                    $(obj).val($(obj).siblings().val());
                } else {
                    if ((jfbalance2 - totalSum) >= 0) {
                        $(obj).siblings().val(deductionAmount2);
                    } else {
                        $(obj).val($(obj).siblings().val());
                    }
                }
            } else {
                if (deductionAmount2 > amount) {
                    $(obj).val($(obj).siblings().val());
                } else {
                    if ((jfbalance2 - totalSum) >= 0) {
                        $(obj).siblings().val(deductionAmount2);
                    } else {
                        $(obj).val($(obj).siblings().val());
                    }
                }
            }
            countMoney()
        } else {
            $(obj).val(0);
            countMoney()
        }
    } else {
        $(obj).val(0);
        layer.alert("没有积分可以进行抵扣！");
        return false;
    }
}

function onchangeDepositAmount2(obj) {
    var depositAmount2 = parseFloat($(obj).val());
    var moneyBalance2 = parseFloat($("#moneyBalance2").html());
    if (moneyBalance2 > 0) {
        var totalSum = 0;
        $("#inquireAbout .depositAmount2").each(function (index, obj) {
            totalSum += parseFloat($(obj).val());
        });
        if (isNaN(depositAmount2) != true) {
            var amount = parseFloat($(obj).parent().parent().find(".amount2").html());
            if ($('#integral').is(':checked')) {
                var deductionAmount2 = parseFloat($(obj).parent().parent().find(".deductionAmount2").val());
                if ((deductionAmount2 + depositAmount2) > amount) {
                    $(obj).val($(obj).siblings().val());
                } else {
                    if ((moneyBalance2 - totalSum) >= 0) {
                        $(obj).siblings().val(depositAmount2);
                    } else {
                        $(obj).val($(obj).siblings().val());
                    }
                }
            } else {
                if (depositAmount2 > amount) {
                    $(obj).val($(obj).siblings().val());
                } else {
                    if ((moneyBalance2 - totalSum) >= 0) {
                        $(obj).siblings().val(depositAmount2);
                    } else {
                        $(obj).val($(obj).siblings().val());
                    }
                }
            }
            countMoney()
        } else {
            $(obj).val(0);
            countMoney()
        }
    } else {
        $(obj).val(0);
        layer.alert("没有储值可以划扣！");
        return false;
    }
}

function countMoney() {
    var totalSum = 0;
    $("#inquireAbout .deductionAmount2").each(function (index, obj) {
        totalSum += parseFloat($(obj).val());
    });
    $("#inquireAbout .depositAmount2").each(function (index, obj) {
        totalSum += parseFloat($(obj).val());
    });
    $("#arrearsAmount").val(parseFloat($("#chooseDealArrears").html() - totalSum));
}

function decimalNum(obj) {
    obj.value = obj.value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
    obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字
    obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
    if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        obj.value = parseFloat(obj.value);
    }
}